import {
	wsSend
} from '../../request/trade';
import moment from "moment";
import {
	sortReverse
} from "../../utils/auth";
import db from '../../../datastore'
moment.locale('zh-cn')

let fsLists  = [];  //汇总止盈止损
let idLists = [];
let posList = []; // 暂存持仓单列表
let comList = []; // 暂存委托单列表
let fsList = {}; // 暂存止盈止损列表
let fsListt = []; // 暂存止盈止损列表
let traList = []; // 暂存成交明细列表
let fenyelist = [];
let fsListt2 = []; // 暂存止盈止损列表
let fenyelist2 = [];
let floList = []; // 暂存资金流水列表
let hiscList = []; // 暂存委托单历史记录列表
let histList = []; // 暂存历史成交记录列表
let hisoList = []; // 暂存平仓历史记录列表

const state = {
	posRadio2: '1',
	isbuy: '0',
	paneIndex: 1, // 交易面板展示当前内容
	spanel: false, // 是否显示交易面板
	sminipanel: false, // 是否显示mini交易面板
	positionlist: [], // 持仓单列表
	ponIndex: -1, // 持仓单索引
	commissionlist: [], // 委托单列表
	transactionlist: [], // 成交明细列表
	capitalflowlist: [], // 资金流水列表
	hiscommilist: [], // 委托单历史记录列表
	histranslist: [], // 用户历史成交记录列表
	hiscloselist: [], // 用户平仓历史记录列表
	fullstoplist: {}, // 止盈止损列表
	fullstoplistsz: [], //汇总止盈止损列表
	fullstoplist2: [], // 止盈止损列表
	fullfenyelist: [],
	total: 0,
	hisType: 0, // 1为跟单系统中获取的平仓记录
	userTradeList: [], // 跟单系统当前用户交易记录
	delogEve: false, // 下单时弹出确认提示框
	dbclickEve: true, // 双击持仓单平仓
	isPLDel: false, // 是否为用户点击删除
	selName: {}, // 当前选中的持仓合约信息
	winSnap: 0, // 0 主界面切换 1主界面全屏进入迷你 2 主界面非全屏进入迷你
	oldBounds: null, // 旧窗口信息
	Modules: {}, // 止盈止损列表的name 持仓
	Modules2: {}, // 汇总 -止盈止损列表的name 持仓
	switchFlag: true // 止盈止损编辑、更新弹窗
}

const mutations = {
	SET_POSRADIO: (state, val) => {
		state.posRadio2 = val
	},
	SET_ISBUY: (state, num) => {
		state.isbuy = num
	},
	SET_PONINDEX: (state, ponindex) => {
		state.ponIndex = ponindex
	},
	SET_SWITCHFLAG: (state, flag) => {
		state.switchFlag = flag
	},
	SET_OLDBOUNDS: (state, old) => {
		state.oldBounds = old
	},
	SET_WINSNAP: (state, mini) => {
		state.winSnap = mini
	},
	SET_DELOGEVE: (state, dogeve) => {
		state.delogEve = dogeve
	},
	SET_ISPLDEL: (state, stau) => {
		state.isPLDel = stau
	},
	SET_MODULES: (state, module) => {
		state.Modules = module
	},
	SET_MODULES2: (state, module) => {
		state.Modules2 = module
	},
	SET_SELNAME: (state, name) => {
		state.selName = name
	},
	SET_DBCLICKEVE: (state, dbeve) => {
		state.dbclickEve = dbeve
	},
	SET_PANEINDEX: (state, index) => {
		state.paneIndex = index
	},
	SET_HISTYPE: (state, stu) => {
		state.hisType = stu
	},
	SET_USERTRADELIST: (state, list) => {
		state.userTradeList = list
	},
	SET_SPANEL: (state, show) => {
		state.spanel = show
	},
	SET_SMINIPANEL: (state, show) => {
		state.sminipanel = show
	},
	SET_POSITIONLIST: (state, poslist) => {
		state.positionlist = poslist
		// console.log('持仓', state.positionlist)
	},
	SET_COMMISSIONLIST: (state, comlist) => {
		state.commissionlist = comlist
		// console.log(state.commissionlist)
	},
	SET_CJLIST: (state, comlist) => {
		state.transactionlist = comlist
		// console.log(state.commissionlist)
	},
	SET_FULLSTOPLIST: (state, list) => {
		state.fullstoplist = list
	},
	SET_FULLSTOPLISTS: (state, list) => {
		state.fullstoplistsz = list
	},
	SET_FULLSTOPLIST2: (state, list) => {
		state.fullstoplist2 = list
	},
	SET_TOTAL: (state, num) => {
		state.total = num
	},
	SET_FULLFENYELIST: (state, list) => {
		state.fullfenyelist = list
	},
	SET_TRANSACTIONLIST: (state, tralist) => {
		state.transactionlist = tralist
		// console.log(state.transactionlist)
	},
	SET_FLOWLIST: (state, flolist) => {
		state.capitalflowlist = flolist;
		// console.log(state.capitalflowlist)
	},
	SET_HISCOMMILIST: (state, hcomlist) => {
		state.hiscommilist = hcomlist;
		// console.log(state.hiscommilist)
	},
	SET_HISTRANSLIST: (state, htralist) => {
		state.histranslist = htralist;
		// console.log(state.histranslist)
	},
	SET_HISCLOSELIST: (state, closelist) => {
		state.hiscloselist = closelist;
		// console.log(state.hiscloselist)
	}
}

const actions = {
	updateposRadio: ({commit}, val) => {
		commit('SET_POSRADIO', val)
	},
	updateIsbuy: ({
		commit
	}, num) => {
		commit('SET_ISBUY', num)
	},
	updateSwitchFlag: ({
		commit
	}, flag) => {
		commit('SET_SWITCHFLAG', flag)
	},
	updateSelname: ({
		commit
	}, name) => {
		commit('SET_SELNAME', name)
	},
	// 更新持仓列表的选中索引
	updatePonIndex: ({
		commit
	}, index) => {
		commit('SET_PONINDEX', index)
	},
	isShowPanel: ({
		commit
	}, isShow) => {
		commit('SET_SPANEL', isShow)
	},
	isShowMiniPanel: ({
		commit
	}, isShow) => {
		commit('SET_SMINIPANEL', isShow)
	},
	// // 分页处理
	// updateFenye: ({ commit }, {limit, currentpage}) {
	//   for (let i = 0; i< ; i+=2) {
	//     fenyelist.push(_listarr[i])
	//   }
	//   commit('SET_FULLSTOPLIST2', )
	// },
	// 更新持仓单列表
	updatePositionBook: ({
		commit,
		rootGetters
	}, list) => {
		if (list[1] === '5') {
			posList = []
			commit('SET_POSITIONLIST', [])
			// console.log('总数：' + list[2])
			return
		}
		//console.log(list)
		// 解构当前条目数据
		let [, , ID, Name, Business, Quantity, CanBeFlat, AveragePrice, Currency, HandlingFee, ,Biaoshi] = list;
		let obj = null;
		if (ID !== '0') {
			obj = {
				ID, // 明细id
				Name, // 合约名称
				BusCode: Business,
				Business: Business === '1' ? '买' : '卖', // 买卖：1买2卖
				Quantity: Quantity * 1, // 持仓量
				CanBeFlat: CanBeFlat * 1, // 可平量
				AveragePrice: AveragePrice * 1, // 持仓均价（开仓价）
				WinLosses: 0, // 持仓盈亏
				Currency, // 币种
				HandlingFee: HandlingFee * 1 ,// 手续费
				Biaoshi: Biaoshi*1
			}
			// console.log('合约代码信息：', rootGetters.productlist)
			for (const item of rootGetters.productlist) {
				if (item.Name === obj.Name) {
					obj.ContractID = item.ContractID // 期货合约合约ID
					obj.ContractCode = item.ContractCode // 期货合约代号
					obj.Multiplier = item.Multiplier // 客户端合约乘数
					obj.Minimum = item.Minimum // 最小波动
					break;
				}
			}
			posList.push(obj)
		} else {
			// 获取完成后，发送请求获取止盈止损
			commit('SET_POSITIONLIST', posList)
		}
	},
	// 更新止盈止损列表
	updateFullStop: ({commit}, {list, module, flag}) => {
		// fsListt = []
		// 38;1;合约ID;买卖方向;数量;止盈点位;止损点位;止盈金额;止损金额;止盈止损类别;超价点数;明细ID;
		// console.log('modulelist:', modulelist)
		let [, , selfId, ContractId, Business, Quantity, ProfitPoint, LossPoint, ProfitAmount, LossAmount, Type, Super, ID] =
		list;
		// if (list[1] == '5' && list[2] == '0') {
		//   fsList = []
		//   commit('SET_FULLSTOPLIST', fsList)
		//   return
		// }
		if (selfId == '0') {

			if (module.Name) {
				// console.log('当前止盈止损列表fslist', fsListt)
				let _listarr = []

				function unique(arr) {
					return Array.from(new Set(arr))
				}
				_listarr = unique(fsListt)
				for (let i = 0; i < _listarr.length; i++) {
					_listarr[i].Name = module.Name
					_listarr[i].AveragePrice = module.AveragePrice
				}
				let totals = _listarr.length
				if (totals > 6) {
					// 分页
					for (let i = 0; i < _listarr.length; i += 6) {
						fenyelist.push(_listarr.slice(i, i + 6))
					}
					commit('SET_FULLFENYELIST', fenyelist)
				}
				commit('SET_FULLSTOPLIST2', totals > 6 ? fenyelist[0] : _listarr)
				commit('SET_TOTAL', _listarr.length)
				fsListt = []
				fenyelist = []
			}
			commit('SET_FULLSTOPLIST', fsList)
		} else {
			let obj = {}
			obj = {
				selfId,
				ContractId, // 合约ID
				Business: Business === '1' ? '买' : '卖', // 买卖方向：1买2卖
				Quantity: Quantity * 1, // 数量
				ProfitPoint: ProfitPoint * 1, // 止盈点位
				LossPoint: LossPoint * 1, // 止损点位
				ProfitAmount: ProfitAmount * 1, // 止盈金额
				LossAmount: LossAmount * 1, // 止损金额
				Type, // 止盈止损类别
				Super: Super * 1, // ;超价点数
				ID
			}
			if (flag) {
				fsList[ID] = obj
			}
			if (module.Name && ID) fsListt.push(obj)

		}
		
	},
	// 汇总的止盈止损列表
	updateFullStops: ({commit}, {list, module2, flag2}) => {
		// fsListt = []
		// 38;1;合约ID;买卖方向;数量;止盈点位;止损点位;止盈金额;止损金额;止盈止损类别;超价点数;明细ID;
		// console.log('modulelist:', modulelist)
		let [, , selfId, ContractId, Business, Quantity, ProfitPoint, LossPoint, ProfitAmount, LossAmount, Type, Super, ID] =
		list;
		

		// console.log('汇总列表：', fsLists)
		if (selfId == '0') {

			if (module2.Name) {
				// console.log('汇总止盈止损列表fslist2', fsListt2)
				let _listarr = []

				function unique(arr) {
					return Array.from(new Set(arr))
				}
				_listarr = unique(fsListt2)
				for (let i = 0; i < _listarr.length; i++) {
					_listarr[i].Name = module2.Name
					_listarr[i].AveragePrice = module2.AveragePrice
				}
				let totals = _listarr.length
				if (totals > 6) {
					// 分页
					for (let i = 0; i < _listarr.length; i += 6) {
						fenyelist2.push(_listarr.slice(i, i + 6))
					}
					commit('SET_FULLFENYELIST', fenyelist2)
				}
				commit('SET_FULLSTOPLIST2', totals > 6 ? fenyelist2[0] : _listarr)
				commit('SET_TOTAL', _listarr.length)
				fsListt2 = []
				fenyelist2 = []
			}
			//commit('SET_FULLSTOPLISTS', fsLists)
			
		} else {
			let obj = {}
			obj = {
				selfId,
				ContractId, // 合约ID
				Business: Business === '1' ? '买' : '卖', // 买卖方向：1买2卖
				Quantity: Quantity * 1, // 数量
				ProfitPoint: ProfitPoint * 1, // 止盈点位
				LossPoint: LossPoint * 1, // 止损点位
				ProfitAmount: ProfitAmount * 1, // 止盈金额
				LossAmount: LossAmount * 1, // 止损金额
				Type, // 止盈止损类别
				Super: Super * 1, // ;超价点数
				ID
			}
			if (selfId) {
				fsLists[ContractId] = obj
			}
			if (module2.Name && ContractId) fsListt2.push(obj)

		}
		
	},
	
	// 汇总的止盈止损列表（新）
	updateFullStopsNew: ({commit,state}, list) => {
		// fsListt = []
		// 35;1;止盈止损ID;合约ID;买卖方向;数量;止盈点位;止损点位;止盈金额;止损金额;止盈止损类别;超价点数;
		// console.log('modulelist:', modulelist)
		let [, , selfId, ContractId, Business, Quantity, ProfitPoint, LossPoint, ProfitAmount, LossAmount, Type, Super, ID] =
		list;

		if (list[1] === '5') {
			fsLists = []
			commit('SET_FULLSTOPLISTS', [])
			return
		}
		else{
			if (selfId == '0') {
				commit('SET_FULLSTOPLISTS', fsLists)
				
				let modules2 = state.Modules2
				
				if(modules2&&modules2.moduleID==0){
					let BusCode      = modules2.BusCode==1?'买':'卖';
					let contractID   = modules2.ContractID;
					let AveragePrice = modules2.AveragePrice;
					let Name         = modules2.Name;
					
					let temp_lists = fsLists.filter(item => item.ContractId === contractID && item.Business === BusCode)
					let temp_lists1 = temp_lists.map(item=>{
						item.AveragePrice = AveragePrice 
						item.Name = Name
						return item
					})
					
					commit('SET_FULLSTOPLIST2', temp_lists1)
				}
				
				return
			} 
			else {

				let obj = {
					selfId,
					ContractId, // 合约ID
					Business: Business === '1' ? '买' : '卖', // 买卖方向：1买2卖
					Quantity: Quantity * 1, // 数量
					ProfitPoint: ProfitPoint * 1, // 止盈点位
					LossPoint: LossPoint * 1, // 止损点位
					ProfitAmount: ProfitAmount * 1, // 止盈金额
					LossAmount: LossAmount * 1, // 止损金额
					Type, // 止盈止损类别
					Super: Super * 1, // ;超价点数
					ID
				}
				fsLists.push(obj)
				return
			}
		}
	},
	// 获取明细-删除成功后的止盈止损
	updateDeletStop: ({commit}, id) => {
		for(var key in fsList) {
			if (id === fsList[key].selfId) {
				delete fsList[key]
			}
		}
	},
	// 获取汇总-删除成功后的止盈止损
	updateDeletStops: ({commit}, id) => {
		for(var key in fsLists) {
			if (id === fsLists[key].selfId) {
				delete fsLists[key]
			}
		}
	},
	// 更新委托单列表
	updateOrderTicket: ({
		commit
	}, item) => {
		if (item[1] === '5') {
			// 当前为total数据时，先初始化
			comList = []
			commit('SET_COMMISSIONLIST', [])
			// console.log('总数：' + item[2])
			return
		}
		// 解构当前条目数据
		let [, , ID, Numbering, Name, Business, Kaiping, Status, ReportPrice, ReportHand, UnpaidLot, AveragePrice, DateTime,
			UnitLogo, Detailed, LastTime, Category, Currency, cancel_flag
		] = item
		let cobj = null;
		// console.log(item)
		// 判断是否传输完毕
		if (ID !== '0') {
			// console.log('当前时间戳：', DateTime)
			let DTimestamp = DateTime
			if (DateTime && DateTime !== '0') {
				DateTime = moment(DateTime * 1000).format('YYYY-MM-DD HH:mm');
			}
			// console.log('当前时间：', DateTime)
			if (LastTime && LastTime !== '0') {
				LastTime = moment(LastTime * 1000).format('YYYY-MM-DD HH:mm');
			}
			cobj = {
				ID,
				Numbering, // 报单编号
				Name, // 合约名称
				Business: Business === '1' ? '买' : '卖', // 买卖方向：1买2卖
				Kaiping: Kaiping === '1' ? '开仓' : '平仓', // 开平：1开仓2平仓
				Status, // 状态
				ReportPrice: ReportPrice * 1, // 报单价格
				ReportHand: ReportHand * 1, // 报单手数
				UnpaidLot: UnpaidLot * 1, // 未成交手数
				AveragePrice: AveragePrice * 1, // 成交均价
				DateTime: DateTime, // 报单时间
				DTimestamp: DTimestamp * 1, // 时间戳
				UnitLogo, // 平仓挂单标志位
				Detailed, // 详细描述
				LastTime: LastTime, // 最后成交时间
				Category, // 开平仓类别
				Currency, // 币种
				cancel_flag //撤单标志
			}
			comList.push(cobj)
		} else {
			// 传输完毕
			let sortArr = sortReverse(comList)
			console.log(sortArr)
			commit('SET_COMMISSIONLIST', sortArr)
		}
	},
	// 更新成交明细列表
	updateTransactionDetails: ({
		commit,
		state
	}, detail) => {
		// 传输开始，先清列表
		if (detail[1] === '5') {
			// 当前为total数据时，先初始化
			traList = []

			commit('SET_TRANSACTIONLIST', [])
			// console.log('总数：' + detail[2])
			return
		}
		
		// 25;5;0; 开始  25;1;0; 结束

		let [, ttype, Id, Name, Business, Kaiping, AveragePrice, NumberOfLots, TransTime, TransNo, OpenPrice, PaL,
			Numbering, Currency, YtSign, HandlingFee, ,OpenNO,OpenTime
		] = detail;
		let tobj = null;

		// 初始化存入
		if (Id !== '0') {
			// 25;1;895865;纽约铜2003;1;1;2.78300;1;1576147747;2019121200001387;0.00000;0;895865;3;0;50.00;
			let curText = Currency == "1" ? '人民币' : (Currency === '2' ? '港币' : '美元');

			let Timestamp = TransTime;
			if (TransTime && TransTime !== '0') {
				TransTime = moment(TransTime * 1000).format('YYYY-MM-DD HH:mm');
			}
			if (OpenTime && OpenTime !== '0') {
				OpenTime = moment(OpenTime * 1000).format('YYYY-MM-DD HH:mm');
			}
			tobj = {
				Id, // id
				Name, // 合约名称
				Business: Business === '1' ? '买' : '卖', // 买卖方向：1买2卖
				Kaiping: Kaiping === '1' ? '开仓' : '平仓', // 开平：1开仓2平仓
				AveragePrice: AveragePrice * 1, // 成交均价
				NumberOfLots, // 成交手数
				Timestamp, // 时间戳
				TransTime: TransTime, // 成交时间
				TransNo, // 成交编号
				OpenPrice: OpenPrice * 1, // 建仓均价
				PaL: PaL === '0' ? '--' : PaL, // 盈亏
				Numbering, // 报单编号
				Currency, // 币种
				curText, // 币种
				YtSign, // 昨今标志
				HandlingFee: HandlingFee * 1 ,// 手续费
				OpenNO, //建仓单号
				OpenTime,  //建仓时间
			};

			// 存入暂存变量中
			traList.unshift(tobj);
		} else {
			//console.log(traList);
			//let sortArr = sortReverse(traList)
			//console.log(sortArr);
			commit('SET_TRANSACTIONLIST', traList)
		}


		// a、增加 25;2;ID;合约名称;买卖方向;开平;成交均价;成交手数;成交时间;成交编号;建仓均价;盈亏;报单编号;币种;昨今标志;手续费;
		if (ttype === '2') {
			conolse.log("dfdfd");
			let curText = Currency === 1 ? '人民币' : (Currency === 2 ? '港币' : '美元');
			let Timestamp = TransTime;
			if (TransTime && TransTime !== '0') {
				TransTime = moment(TransTime * 1000).format('YYYY-MM-DD HH:mm');
			}
			if (OpenTime && OpenTime !== '0') {
				OpenTime = moment(OpenTime * 1000).format('YYYY-MM-DD HH:mm');
			}
			tobj = {
				Id, // id
				Name, // 合约名称
				Business: Business === '1' ? '买' : '卖', // 买卖方向：1买2卖
				Kaiping: Kaiping === '1' ? '开仓' : '平仓', // 开平：1开仓2平仓
				AveragePrice: AveragePrice * 1, // 成交均价
				NumberOfLots, // 成交手数
				Timestamp, // 时间戳
				TransTime: TransTime, // 成交时间
				TransNo, // 成交编号
				OpenPrice: OpenPrice * 1, // 建仓均价
				PaL, // 盈亏
				Numbering, // 报单编号
				Currency, // 币种
				curText, // 币种
				YtSign, // 昨今标志
				HandlingFee: HandlingFee * 1 ,// 手续费
				OpenNO, //建仓单号
				OpenTime,  //建仓时间
			};

			traList = state.transactionlist.map(v => {
				return { ...v
				}
			});
			conolse.log(traList);
			traList.unshift(tobj);
			conolse.log(traList);
			commit('SET_TRANSACTIONLIST', traList);
		}

		// c、更新 25;4;ID;合约名称;买卖方向;开平;成交均价;成交手数;成交时间;成交编号;建仓均价;盈亏;报单编号;币种;昨今标志;手续费;
		if (ttype === '4') {
			let curText = Currency === 1 ? '人民币' : (Currency === 2 ? '港币' : '美元');
			let Timestamp = TransTime;
			if (TransTime && TransTime !== '0') {
				TransTime = moment(TransTime * 1000).format('YYYY-MM-DD HH:mm');
			}
			if (OpenTime && OpenTime !== '0') {
				OpenTime = moment(OpenTime * 1000).format('YYYY-MM-DD HH:mm');
			}
			tobj = {
				Id, // id
				Name, // 合约名称
				Business: Business === '1' ? '买' : '卖', // 买卖方向：1买2卖
				Kaiping: Kaiping === '1' ? '开仓' : '平仓', // 开平：1开仓2平仓
				AveragePrice: AveragePrice * 1, // 成交均价
				NumberOfLots, // 成交手数
				Timestamp, // 时间戳
				TransTime: TransTime, // 成交时间
				TransNo, // 成交编号
				OpenPrice: OpenPrice * 1, // 建仓均价
				PaL, // 盈亏
				Numbering, // 报单编号
				Currency, // 币种
				curText, // 币种
				YtSign, // 昨今标志
				HandlingFee: HandlingFee * 1 ,// 手续费
				OpenNO,  //建仓单号
				OpenTime,  //建仓时间
			};

			let upList = state.transactionlist.map(v => {
				if (v.Id === Id) {
					return tobj
				} else {
					return { ...v
					}
				}

			});

			// 将当前更新列表替换入state中
			commit('SET_TRANSACTIONLIST', upList);
		}

		// b、删除 25;3;ID;
		if (ttype === '3') {
			let delList = state.transactionlist.map(v => {
				return { ...v
				}
			});

			for (let [index, val] of delList.entries()) {
				// console.log(index + ':' + val);
				if (val.Id === Id) {
					delList.splice(index, 1);
				}
			}

			// 将删除后的成交明细列表存入state中
			commit('SET_TRANSACTIONLIST', delList);
		}
	},
	// 更新历史资金流水
	updateCapitalFlow: ({
		commit
	}, flow) => {
		if (flow[1] === '2') {
			// console.log('总数为：' + flow[2])
			floList = [];
			commit('SET_FLOWLIST', [])
			return
		}

		let [, , FlowId, DateTime, FlowType, SingleNo, FlowSum, Balance, Currency] = flow;
		if (FlowId === '0') {
			commit('SET_FLOWLIST', floList);
		} else {
			// Currency = Currency == "1" ? '人民币' : (Currency === '2' ? '港币' : '美元');
			if (DateTime && DateTime !== '0') {
				DateTime = moment(DateTime * 1000).format('YYYY-MM-DD HH:mm');
			}
			let obj = {
				FlowId, // 流水id
				DateTime: DateTime, // 时间
				FlowType, // 流水类别
				SingleNo, // 单号
				FlowSum: FlowSum * 1, // 流水金额
				Balance: Balance * 1, // 余额
				Currency // 货币名称
			};
			floList.unshift(obj);
		}
	},
	// 更新委托单历史记录
	updateHisCommission: ({
		commit
	}, clist) => {
		if (clist[1] === '2') {
			// console.log('总数为：' + clist[2])
			hiscList = [];
			commit('SET_HISCOMMILIST', [])
			return
		}

		let [, , Numbering, Name, Currency, Business, Kaiping, Status, ReportPrice, OrdersNo, TransNo, AveragePrice,
			DateTime, CommiType
		] = clist;
		if (Numbering == '0') {
			commit('SET_HISCOMMILIST', hiscList);
		} else {
			// Currency = Currency == '1' ? '人民币' : (Currency === "2" ? '港币' : '美元');
			if (DateTime && DateTime !== '0') {
				DateTime = moment(DateTime * 1000).format('YYYY-MM-DD HH:mm');
			}
			let obj = {
				Numbering, // 报单编号
				Name, // 合约名称
				Currency, // 币种
				Business: Business === '1' ? '买' : '卖', // 买卖方向：1买2卖
				Kaiping: Kaiping === '1' ? '开仓' : '平仓', // 开平：1开仓2平仓
				Status, // 报单状态
				ReportPrice: ReportPrice * 1, // 报单价格
				OrdersNo: OrdersNo * 1, // 报单数量
				TransNo: TransNo * 1, // 成交数量
				AveragePrice: AveragePrice * 1, // 成交均价
				DateTime: DateTime, // 报单时间
				CommiType // 委托类型
			}
			// console.log(obj)
			hiscList.unshift(obj)
		}
	},
	// 更新用户历史成交记录
	updateHisTransaction: ({
		commit
	}, tlist) => {
		if (tlist[1] === '2') {
			// console.log('总数为：' + tlist[2])
			histList = [];
			commit('SET_HISTRANSLIST', [])
			return
		}

		let [, , TranId, Name, Currency, Business, Kaiping, AveragePrice, TransNo, HandlingFee, TranTime, Numbering] =
		tlist;

		if (TranId === '0') {
			commit('SET_HISTRANSLIST', histList);
		} else {
			// Currency = Currency === 1 ? '人民币' : (Currency === 2 ? '港币' : '美元');
			if (TranTime && TranTime !== '0') {
				TranTime = moment(TranTime * 1000).format('YYYY-MM-DD HH:mm');
			}
			let obj = {
				TranId, // 成交编号
				Name, // 合约名称
				Currency, // 币种
				Business: Business === '1' ? '买' : '卖', // 买卖方向：1买2卖
				Kaiping: Kaiping === '1' ? '开仓' : '平仓', //开平标志：1开仓2平仓
				AveragePrice: AveragePrice * 1, // 成交价格
				TransNo: TransNo * 1, // 成交数量
				HandlingFee: HandlingFee * 1, // 手续费
				TranTime: TranTime, // 报单时间
				Numbering // 报单编号
			}

			histList.unshift(obj)
		}
	},
	// 更新用户平仓历史记录
	updateHisClose: ({
		commit,
		state
	}, olist) => {
		if (olist[1] === '2') {
			// console.log('总数为：' + olist[2])
			hisoList = [];
			commit('SET_HISCLOSELIST', [])
			commit('SET_USERTRADELIST', [])
			return
		}

		// 43;1;平仓ID;合约名称;币种;买卖方向;开仓均价;平仓价格;平仓手数;平仓盈亏;平仓时间戳;平仓成交号;
		let [, , CloseId, Name, Currency, Business, OpeningPrice, ClosingPrice, ClosedLot, ClosePL, CloseTime, CloseNo] =
		olist;

		if (CloseId === '0') {
			// 如果判断为1，即为跟单系统中查询的用户平仓记录
			if (state.hisType === 1) {
				commit('SET_USERTRADELIST', hisoList);
				commit("SET_HISTYPE", 0); // 获取之后自动恢复默认
			} else {
				commit('SET_HISCLOSELIST', hisoList);
			}

		} else {
			// Currency = Currency === 1 ? '人民币' : (Currency === 2 ? '港币' : '美元');
			if (CloseTime && CloseTime !== '0') {
				CloseTime = moment(CloseTime * 1000).format('YYYY-MM-DD HH:mm');
			}
			let obj = {
				CloseId, // 平仓ID
				Name, // 合约名称
				Currency, // 币种
				Business: Business === '1' ? '买' : '卖', // 买卖方向：1买2卖
				OpeningPrice: OpeningPrice * 1, // 开仓均价
				ClosingPrice: ClosingPrice * 1, // 平仓价格
				ClosedLot: ClosedLot * 1, // 平仓手数
				ClosePL: ClosePL * 1, // 平仓盈亏
				CloseTime: CloseTime, // 平仓时间
				CloseNo // 平仓成交号
			}

			hisoList.unshift(obj)
		}
	},
	// 持仓列表数据更新(增/删/更新)
	holdAPosition: ({
		commit,
		state,
		rootGetters
	}, hlist) => {
		// a、增加
		// 23;2;ID;合约名称;买卖;持仓量;可平量;持仓均价;币种;手续费;合约ID;标识;
		// b、删除
		// 23;3;ID;
		// c、更新
		// 23;4;ID;合约名称;买卖;持仓量;可平量;持仓均价;币种;手续费;合约ID;标识;
		
		//console.log(hlist);

		let [, type, ID, Name, Business, Quantity, CanBeFlat, AveragePrice, Currency, HandlingFee,  , Biaoshi] = hlist

		let updateData = [];
		if (Business === '买') {
			Business = '1'
		}
		if (Business === '卖') {
			Business = '2'
		}

		// 新增
		if (type === '2') {
			for (const item of state.positionlist) {
				// console.log(item)
				let nwoObj = {
					...item
				}
				updateData.push(nwoObj)
			}
			let obj = {
				ID,
				Name, // 合约名称
				BusCode: Business,
				Business: Business === '1' ? '买' : '卖', // 买卖：1买2卖
				Quantity: Quantity * 1, // 持仓量
				CanBeFlat: CanBeFlat * 1, // 可平量
				AveragePrice: AveragePrice * 1, // 持仓均价（开仓价）
				Currency, // 币种
				HandlingFee: HandlingFee * 1, // 手续费
				Biaoshi: Biaoshi * 1, // 手续费
			}
			for (const item of rootGetters.productlist) {
				if (item.Name === obj.Name) {
					obj.ContractID = item.ContractID // 期货合约合约ID
					obj.ContractCode = item.ContractCode // 期货合约代号
					obj.Multiplier = item.Multiplier // 客户端合约乘数
					obj.Minimum = item.Minimum // 最小波动
					break;
				}
			}
			//console.log("持仓明细")
			//console.log(obj)
			updateData.push(obj);
		}

		// 删除
		if (type === '3') {
			for (const list of state.positionlist) {
				if (list.ID !== ID) {
					updateData.push({ ...list
					})
				}
			}
		}

		// 更新
		if (type === '4') {
			for (const list of state.positionlist) {
				if (list.ID === ID) {
					updateData.push({
						...list,
						ID,
						Name: Name ? Name : list.Name, // 合约名称
						BusCode: Business ? Business : list.Business,
						Business: Business ? (Business === '1' ? '买' : '卖') : list.Business, // 买卖：1买2卖
						Quantity: Quantity ? Quantity * 1 : list.Quantity, // 持仓量
						CanBeFlat: CanBeFlat ? CanBeFlat * 1 : list.CanBeFlat, // 可平量
						AveragePrice: AveragePrice ? AveragePrice * 1 : list.AveragePrice, // 持仓均价（开仓价）
						Currency: Currency ? Currency : list.Currency, // 币种
						HandlingFee: HandlingFee ? HandlingFee * 1 : list.HandlingFee // 手续费
					})
				} else {
					updateData.push({ ...list
					})
				}
			}
		}
		commit('SET_POSITIONLIST', updateData)
	},
	// 委托列表更新
	holdCommission: ({
		commit,
		state
	}, hlist) => {
		// a、增加
		// 24;2;ID;报单编号;合约名称;买卖方向;开平;状态;报单价格;报单手数;未成交手数;成交均价;报单时间;平仓挂单标志位;详细描述;最后成交时间;开平仓类别;币种;撤单标志;
		// 24;2;843247;843247;小道指1909;1;1;指令提交成功;26261;1;1;0;1564969365;0;指令提交成功;0;手动;3;
		// b、删除
		// 24;3;ID;
		// c、更新
		// 24;4;ID;报单编号;合约名称;买卖方向;开平;状态;报单价格;报单手数;未成交手数;成交均价;报单时间;平仓挂单标志位;详细描述;最后成交时间;开平仓类别;币种;撤单标志;
		let [, type, ID, Numbering, Name, Business, Kaiping, Status, ReportPrice, ReportHand, UnpaidLot, AveragePrice,
			DateTime, UnitLogo, Detailed, LastTime, Category, Currency,cancel_flag
		] = hlist


		let newData = [] // 暂存更新委托列表数据

		// 委托新增
		if (type === '2') {
			for (const item of state.commissionlist) {
				// console.log(item)
				newData.push({
					...item
				})
			}
			let DTimestamp = DateTime
			if (DateTime && DateTime !== '0') {
				DateTime = moment(DateTime * 1000).format('YYYY-MM-DD HH:mm');
			}
			if (LastTime && LastTime !== '0') {
				LastTime = moment(LastTime * 1000).format('YYYY-MM-DD HH:mm');
			}
			let cobj = {
				ID,
				Numbering, // 报单编号
				Name, // 合约名称
				Business: Business === '1' ? '买' : '卖', // 买卖方向：1买2卖
				Kaiping: Kaiping === '1' ? '开仓' : '平仓', // 开平：1开仓2平仓
				Status, // 状态
				ReportPrice: ReportPrice * 1, // 报单价格
				ReportHand, // 报单手数
				UnpaidLot, // 未成交手数
				AveragePrice: AveragePrice * 1, // 成交均价
				DateTime: DateTime, // 报单时间
				DTimestamp: DTimestamp * 1, // 时间戳
				UnitLogo, // 平仓挂单标志位
				Detailed, // 详细描述
				LastTime: LastTime, // 最后成交时间
				Category, // 开平仓类别
				Currency, // 币种
				cancel_flag //撤单标志
			}
			newData.push(cobj)
			// console.log('当前委托新增：', newData)
		}

		// 委托删除
		if (type === '3') {
			for (const list of state.commissionlist) {
				if (list.ID !== ID) {
					newData.push({ ...list
					})
				}
			}
			// console.log('当前委托删除：', newData)
		}

		// 委托更新
		if (type === '4') {
			for (const list of state.commissionlist) {
				if (list.ID === ID) {
					let DTimestamp = DateTime
					if (DateTime && DateTime !== '0') {
						DateTime = moment(DateTime * 1000).format('YYYY-MM-DD HH:mm');
					}
					if (LastTime && LastTime !== '0') {
						LastTime = moment(LastTime * 1000).format('YYYY-MM-DD HH:mm');
					}
					newData.push({
						...list,
						Business: Business ? (Business === '1' ? '买' : '卖') : list.Business, // 买卖：1买2卖
						Kaiping: Kaiping ? (Kaiping === '1' ? '开仓' : '平仓') : list.Kaiping, // 开平：1开仓2平仓
						Status: Status ? Status : list.Status, // 状态
						ReportPrice: ReportPrice ? ReportPrice * 1 : list.ReportPrice, // 报单价格
						ReportHand: ReportHand ? ReportHand * 1 : list.ReportHand, // 报单手数
						UnpaidLot: UnpaidLot ? UnpaidLot * 1 : list.UnpaidLot, // 未成交手数
						AveragePrice: AveragePrice ? AveragePrice * 1 : list.AveragePrice, // 成交均价
						DTimestamp: DTimestamp ? DTimestamp * 1 : list.DTimestamp, // 时间戳
						DateTime: DateTime ? DateTime : list.DateTime, // 报单时间
						UnitLogo: UnitLogo ? UnitLogo : list.UnitLogo, // 平仓挂单标志位
						Detailed: Detailed ? Detailed : list.Detailed, // 详细描述
						LastTime: LastTime ? LastTime : list.LastTime, // 最后成交时间
						Category: Category ? Category : list.Category, // 开平仓类别
						Currency: Currency ? Currency : list.Currency, // 币种
						cancel_flag: cancel_flag ? cancel_flag : list.cancel_flag // 币种
					})
				} else {
					newData.push({ ...list
					})
				}
			}
			// console.log('当前委托更新：', newData)
		}

		let sortArr = sortReverse(newData)
		
		

		commit('SET_COMMISSIONLIST', sortArr)
	},


	// 成交更新

	holdCj: ({
		commit,
		state
	}, hlist) => {


		// 		a、增加
		//       25;2;ID;合约名称;买卖方向;开平;成交均价;成交手数;成交时间;成交编号;建仓均价;盈亏;报单编号;币种;昨今标志;手续费;
		//       b、删除
		//       25;3;ID;
		//       c、更新
		//       25;4;ID;合约名称;买卖方向;开平;成交均价;成交手数;成交时间;成交编号;建仓均价;盈亏;报单编号;币种;昨今标志;手续费;



		// 25;2;895893;新华A501912; 1;        1;       13812.5;       1;       1576150972;         ;     0;     0;   895893;      3;      0;      50.00;
		let [, ttype, Id, Name, Business, Kaiping, AveragePrice, NumberOfLots, TransTime, TransNo, OpenPrice, PaL,
			Numbering, Currency, YtSign, HandlingFee, , OpenNo, OpenTime
		] = hlist;
		let newData = [] // 暂存更新委托列表数据

		// 成交新增
		if (ttype === '2') {
			for (const item of state.transactionlist) {
				// console.log(item)
				newData.push({
					...item
				})
			}


			let curText = Currency === 1 ? '人民币' : (Currency === 2 ? '港币' : '美元');

			let Timestamp = TransTime;
			if (TransTime && TransTime !== '0') {
				TransTime = moment(TransTime * 1000).format('YYYY-MM-DD HH:mm');
			}
			if (OpenTime && OpenTime !== '0') {
				OpenTime = moment(OpenTime * 1000).format('YYYY-MM-DD HH:mm');
			}
			//console.log(TransNo)
			let cobj = {
				Id, // id
				Name, // 合约名称
				Business: Business === '1' ? '买' : '卖', // 买卖方向：1买2卖
				Kaiping: Kaiping === '1' ? '开仓' : '平仓', // 开平：1开仓2平仓
				AveragePrice: AveragePrice * 1, // 成交均价
				NumberOfLots, // 成交手数
				Timestamp, // 时间戳
				TransTime: TransTime, // 成交时间
				TransNo, // 成交编号
				OpenPrice: OpenPrice * 1, // 建仓均价
				// PaL, // 盈亏
				PaL: PaL === '0' ? '--' : PaL, // 盈亏

				Numbering, // 报单编号
				Currency, // 币种
				curText, // 币种
				YtSign, // 昨今标志
				HandlingFee: HandlingFee * 1 ,// 手续费
				OpenNo, // 建仓单号
				OpenTime, //建仓时间

			}
			newData.unshift(cobj)
			//console.log('当前委托新增：', newData)
		}

		// 成交删除
		if (ttype === '3') {
			for (const list of state.transactionlist) {
				if (list.Id !== Id) {
					newData.push({ ...list
					})
				}
			}
			// console.log('当前委托删除：', newData)
		}

		// 成交更新
		if (ttype === '4') {
			for (const list of state.transactionlist) {
				if (list.Id === Id) {
					let curText = Currency === 1 ? '人民币' : (Currency === 2 ? '港币' : '美元');

					let Timestamp = TransTime;
					if (TransTime && TransTime !== '0') {
						TransTime = moment(TransTime * 1000).format('YYYY-MM-DD HH:mm');
					}
					if (OpenTime && OpenTime !== '0') {
						OpenTime = moment(OpenTime * 1000).format('YYYY-MM-DD HH:mm');
					}
					newData.push({
						...list,
						Business: Business ? (Business === '1' ? '买' : '卖') : list.Business, // 买卖：1买2卖
						Kaiping: Kaiping ? (Kaiping === '1' ? '开仓' : '平仓') : list.Kaiping, // 开平：1开仓2平仓
						AveragePrice: AveragePrice ? AveragePrice * 1 : list.AveragePrice, // 成交均价
						NumberOfLots: NumberOfLots ? NumberOfLots * 1 : list.NumberOfLots, // 成交手数,
						Timestamp: Timestamp ? Timestamp * 1 : list.Timestamp, // 时间戳
						TransTime: TransTime ? TransTime * 1 : list.TransTime, //成交时间
						TransNo: TransNo ? TransNo * 1 : list.TransNo,
						OpenPrice: OpenPrice ? OpenPrice * 1 : list.OpenPrice,
						PaL: PaL ? PaL * 1 : list.PaL,
						/* 盈亏*/
						Numbering: Numbering ? Numbering * 1 : list.Numbering,
						/* 保单编号*/
						Currency: Currency ? Currency : list.Currency, // 币种
						curText: curText ? curText : list.curText, // 币种
						YtSign: YtSign ? (YtSign === '1' ? '昨' : '卖') : list.YtSign, // 昨今标志
						HandlingFee: HandlingFee ? HandlingFee : list.HandlingFee, // 手续费
						// 	          Status: Status ? Status : list.Status, // 状态
						// 	          ReportPrice: ReportPrice ? ReportPrice * 1 : list.ReportPrice, // 报单价格
						// 	          ReportHand: ReportHand ? ReportHand * 1 : list.ReportHand, // 报单手数
						// 	          UnpaidLot: UnpaidLot ? UnpaidLot * 1 : list.UnpaidLot, // 未成交手数
						//
						// 	          DateTime: DateTime ? DateTime : list.DateTime, // 报单时间
						// 	          UnitLogo: UnitLogo ? UnitLogo : list.UnitLogo, // 平仓挂单标志位
						// 	          Detailed: Detailed ? Detailed : list.Detailed, // 详细描述
						// 	          LastTime: LastTime ? LastTime : list.LastTime, // 最后成交时间
						// 	          Category: Category ? Category : list.Category, // 开平仓类别
						//









					})
				} else {
					newData.push({ ...list
					})
				}
			}
			// console.log('当前委托更新：', newData)
		}

		//let sortArr = sortReverse(newData)

		commit('SET_TRANSACTIONLIST', newData)
	},
	// 提示音效
	playTipsAudio: (type) => {
		const TipsAudio = new Audio();
		let upurl = db.read().get("trades.ttnewplay").value();
		let audioSrc = `${upurl}/bl2/audio/dialog01.mp3`
		if (type === 1) audioSrc = `${upurl}/bl2/audio/dialog01.mp3` // 提示音效（用于后台返回错误）
		if (type === 2) audioSrc = `${upurl}/bl2/audio/open01.mp3` // 开仓音效
		if (type === 3) audioSrc = `${upurl}/bl2/audio/close01.mp3` // 平仓音效
		if (type === 4) audioSrc = `${upurl}/bl2/audio/success01.mp3` // 成功音效
		if (type === 5) audioSrc = `${upurl}/bl2/audio/error01.wav` // 失败音效
		if (type === 5) audioSrc = `${upurl}/bl2/audio/error01.wav` // 失败音效
		if (type === 6) audioSrc = `${upurl}/bl2/audio/click01.mp3` // 点击音效
		if (type === 7) audioSrc = `${upurl}/bl2/audio/01.mp3` // 建空单成功
		if (type === 8) audioSrc = `${upurl}/bl2/audio/02.mp3` // 建多单成功
		if (type === 9) audioSrc = `${upurl}/bl2/audio/03.mp3` // 平多单成功
		if (type === 10) audioSrc = `${upurl}/bl2/audio/04.mp3` // 平空单成功
		console.log(audioSrc)
		TipsAudio.src = audioSrc;
		TipsAudio.play()
	}
}

export default {
	namespaced: true,
	state,
	mutations,
	actions
}
